package com.wx.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wx.common.utils.DateUtil;
import com.wx.common.utils.ImagePathUtil;
import com.wx.mapper.OrderMasterMapper;
import com.wx.mapper.SourceDetailMapper;
import com.wx.pojo.OrderDetail;
import com.wx.pojo.OrderListVO;
import com.wx.pojo.OrderMaster;
import com.wx.pojo.OrderVo;
import com.wx.pojo.Source;
import com.wx.pojo.SourceDetail;
import com.wx.service.OrderDetailService;
import com.wx.service.OrderMasterService;
import com.wx.service.SourceDetailService;
/**
 * 订单
 * @author gzcadmin
 *
 */
@Service
public class OrderMasterServiceImpl implements OrderMasterService {

	@Autowired
	private OrderMasterMapper orderMasterMapper;
	
	@Autowired
	private SourceDetailMapper sourceDetailMapper;
	
	@Autowired 
	private SourceDetailService sourceDetailService;
	
	@Autowired
	private OrderDetailService orderDetailService;

	/**
	 * 创建订单信息
	 */
	@Override
	public String createOrderMaster(OrderVo orderVO) {
		OrderMaster orderMaster=orderVO.getOrderMaster();
		List<OrderDetail>orderDetailList=orderVO.getOrderDetailList();
		Double orderAmount=0.0;
		
		for (OrderDetail orderDetail : orderDetailList) {
			//从后台查询出商品单价
			SourceDetail source=sourceDetailService.findSourceById(orderDetail.getSourceId());
			if(source!=null) {
				//计算单个商品的总价
				orderDetail.setSourcePrice(source.getSource_price());
				//计算订单总价
				orderAmount+=source.getSource_price()*orderDetail.getSourceCount();
			}
		}
		
		orderMaster.setOrderAmount(orderAmount);
		//创建订单信息，存入数据库
		orderMasterMapper.createOrder(orderMaster);
		//判断是否存入数据库成功
		if(orderMaster.getOrderMasterId()!=null || orderMaster.getOrderMasterId()!="") {
			//创建订单中商品的详细信息并存入数据库
			Boolean flag=orderDetailService.createOrderDetailForm(orderDetailList,orderMaster.getOrderMasterId());
			if(flag==true) {
				return orderMaster.getOrderMasterId();//订单中商品详细信息存入数据库成功
			}else if(flag==false) {
				return null;//失败
			}
		}
		return null;//订单信息存入数据库失败
	}

	/**
	 * 通过用户查询商品信息
	 */
	@Override
	public List<OrderListVO> findOrderByUserId(String userId) {
		
		List<OrderMaster> orderMasterList  = orderMasterMapper.findOrderByUserId(userId);
		
		if(orderMasterList.isEmpty() || orderMasterList.size()<0) {
			return null;
		}
		
		List<OrderListVO> orderListVOList=new ArrayList<>();
		
		for (OrderMaster orderMaster : orderMasterList) {
			OrderListVO orderListVO=new OrderListVO();
			List<OrderDetail> orderDetails=orderDetailService.findOrderDetailByOrderId(orderMaster.getOrderMasterId());
			//修改图片路径
			String path="";
			for (OrderDetail orderDetail : orderDetails) {
				path+=orderDetail.getSourceIcon()+",";
			}
			if(path!=""&&path!=null) {
				path=path.substring(0, path.length()-1);
				String[] orderImage=ImagePathUtil.sourceImagePath(path);
				orderListVO.setSourceImage(orderImage);
			}
//			String Id="OD"+orderMaster.getOrderMasterId().replaceAll("-", "");
			//改变订单id，显示到前端的订单id
			String Id="OD"+orderMaster.getOrderMasterId();
			orderListVO.setOrderId(Id);
			orderListVO.setOrder_amount(orderMaster.getOrderAmount());
			orderListVO.setPay_status(orderMaster.getPay_status());
			
			orderListVO.setOrder_time(DateUtil.DateToString(orderMaster.getCreateTime()));
			orderListVO.setOrderNote(orderMaster.getOrderNote());
			orderListVO.setOrder_status(orderMaster.getOrder_status());
			orderListVOList.add(orderListVO);
		}
		
		return orderListVOList;
	}

	/**
	 * 取消订单
	 */
	@Override
	public void removeOrder(String orderId) {
		List<OrderDetail> orderDetailList=orderDetailService.findOrderDetailByOrderId(orderId);
		if(!orderDetailList.isEmpty() || orderDetailList.size()>0) {
			for (OrderDetail orderDetail : orderDetailList) {
				SourceDetail sourceDetail=sourceDetailService.findSourceById(orderDetail.getSourceId());
				if(sourceDetail!=null) {
					Source source=new Source();
					source.setSourceId(orderDetail.getSourceId());
					source.setSource_count(orderDetail.getSourceCount()+sourceDetail.getSource_count());
					sourceDetailMapper.updateSourceCountBysourceId(source);
				}else {
					//商品id为空
				}
			}
		}
		orderMasterMapper.removeOrder(orderId);
		
	}
	/**
	 * 支付
	 */
	@Override
	public Boolean payOrder(String orderMasterId) {
		OrderMaster orderMaster=orderMasterMapper.findOrderByOrderId(orderMasterId);
		if(orderMaster.getOrder_status()==0) {
			orderMasterMapper.payOrder(orderMasterId);
			return true;
		}else {
			return false;
		}
		
	}
	/**
	 * 完成订单，确认服务完成
	 */
	@Override
	public Boolean finishOrder(String orderMasterId) {
		OrderMaster orderMaster=orderMasterMapper.findOrderByOrderId(orderMasterId);
		if(orderMaster.getOrder_status()==0 && orderMaster.getPay_status()==1) {
			orderMasterMapper.finishOrder(orderMasterId);
			return true;
		}else {
			return false;
		}
	}
	
	/**
	 * 查询完结订单
	 */
	@Override
	public List<OrderListVO> findOrderFinishByUserId(String userId) {
		List<OrderMaster> orderMasterList  = orderMasterMapper.findOrderFinishByUserId(userId);
		
		if(orderMasterList.isEmpty() || orderMasterList.size()<0) {
			return null;
		}
		
		List<OrderListVO> orderListVOList=new ArrayList<>();
		
		for (OrderMaster orderMaster : orderMasterList) {
			OrderListVO orderListVO=new OrderListVO();
			List<OrderDetail> orderDetails=orderDetailService.findOrderDetailByOrderId(orderMaster.getOrderMasterId());
			String path="";
			for (OrderDetail orderDetail : orderDetails) {
				path+=orderDetail.getSourceIcon()+",";
			}
			if(path!=""&&path!=null) {
				path=path.substring(0, path.length()-1);
				String[] orderImage=ImagePathUtil.sourceImagePath(path);
				orderListVO.setSourceImage(orderImage);
			}
//			String Id="OD"+orderMaster.getOrderMasterId().replaceAll("-", "");
			String Id="OD"+orderMaster.getOrderMasterId();
			orderListVO.setOrderId(Id);
			orderListVO.setOrder_amount(orderMaster.getOrderAmount());
			orderListVO.setPay_status(orderMaster.getPay_status());
			
			orderListVO.setOrder_time(DateUtil.DateToString(orderMaster.getCreateTime()));
			orderListVO.setOrderNote(orderMaster.getOrderNote());
			orderListVO.setOrder_status(orderMaster.getOrder_status());
			orderListVOList.add(orderListVO);
		}
		
		return orderListVOList;
	}

	
	
	


	
	
}
